export default function debouncer(callback: any, delay=100) {
    let last = 0
    let timer: any = null
    let func:any = function() {
        if(timer!=null) {
            clearTimeout(timer)
            timer = null
        }
        let now = Date.now()
        if(now-last>delay) {
            callback.apply(null, arguments)
            last = now
        }
        else {
            timer = setTimeout(()=>{
                callback.apply(null, arguments)
                clearTimeout(timer)
                timer = null
            },delay)
        }
    }
    func.clear = function() {
        if(timer!=null) {
            clearTimeout(timer)
            timer = null
        }
    }

    return func
}